﻿using System;
using System.Web;
using System.Web.UI.WebControls;
using Core.Library;
using DataObject;
using DataObject.Category;
using DataObject.Customer;
using DataObject.Order;
using DataObject.OrderDetail;
using DataObject.Product;

public partial class Controls_Customer_Order : System.Web.UI.UserControl
{
    protected CustomerObject Customer;
    protected string IsShowOrderDetail = "none";
    protected int OrderId;
    protected int PageIndex;
    protected int PageSize;

    protected void Page_Load(object sender, EventArgs e)
    {
        OrderId   = Utils.GetIdFromRequestString(Utils.OrderId, 0);
        PageIndex = Utils.GetIdFromRequestString(Utils.PageIndex, 0);
        PageSize  = Utils.GetIdFromRequestString(Utils.PageSize, SettingValues.PageSize_10);

        if (HttpContext.Current.Session["e3group.denhongphuc.customer"] != null)
            Customer = (CustomerObject)Session["e3group.denhongphuc.customer"];            
        else
            Response.Redirect(UrlHelpers.Home); 

        if (!IsPostBack)
        {
            ListOrderByCustomer();

            if (OrderId > 0)
                ListOrderDetailByOrderId();
        }

        Page.Title = string.Format(".: Quản lý đơn hàng {0} :.", Constants.SITE_TITLE);    
    }

    private void ListOrderByCustomer()
    {
        if (HttpContext.Current.Session["e3group.denhongphuc.customer"] != null)
        {
            var totalRecords = 0;
            var lstOrder = OrderController.GetListSearch(Customer.CustomerId, 0, PageIndex, PageSize, ref totalRecords);

            rptOrder.DataSource = lstOrder;
            rptOrder.DataBind();    
        }
    }

    private void ListOrderDetailByOrderId()
    {
        var lstOrderDetail = OrderDetailController.OrderDetail_GetListByOrderId(OrderId);
        if (lstOrderDetail.Count > 0)
        {
            rptOrderDetail.DataSource = lstOrderDetail;
            rptOrderDetail.DataBind();

            IsShowOrderDetail = "block";
        }
        else
        {
            ltMessage.Text = MessageUtility.ShowMessage(null, "Không có sản phẩm nào trong đơn hàng này.", MessageType.Information);                    
            IsShowOrderDetail = "none";
        }
    }

    protected string GetOrderStatus(int status)
    {
        switch (status)
        {
            case (int)OrderStatus.OrderTemp:
                return "Đặt hàng";

            case (int)OrderStatus.OrderComplete:
                return "Đã thanh toán";

            case (int)OrderStatus.OrderDeleted:
                return "Đã xóa";                
        }
        return "-";
    }

    protected void rptOrder_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            var lbDelete = (LinkButton) e.Item.FindControl("lbDelete");
            var status   = ConvertUtility.ToInt32(((HiddenField)e.Item.FindControl("hfStatus")).Value);
            if (status == (int)OrderStatus.OrderComplete || status == (int)OrderStatus.OrderDeleted)
                lbDelete.Visible = false;
        }
    }

    protected void rptOrder_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (string.Equals(e.CommandName, "RemoveOrder"))
        {
            var orderId = ConvertUtility.ToInt32(e.CommandArgument);

            if (orderId > 0)
            {
                var o = OrderController.Order_GetById(orderId);
                if (o != null)
                    OrderController.Order_Update_Status(orderId, (int)OrderStatus.OrderDeleted);

                ltMessage.Text = MessageUtility.ShowMessage(null, "Xóa đơn hàng thành công.", MessageType.Information);
            }
        }
        if (string.Equals(e.CommandName, "SaveOrder"))
        {
            var orderId = ConvertUtility.ToInt32(e.CommandArgument);

            if (orderId > 0)
            {
                var note = ((TextBox) e.Item.FindControl("txtNote")).Text;
                if (!string.IsNullOrEmpty(note))
                    OrderController.Order_Update_Note(orderId, note);
                ltMessage.Text = MessageUtility.ShowMessage(null, "Cập nhật ghi chú đơn hàng thành công.", MessageType.Information);
            }
        }
        ListOrderByCustomer();
    }

    protected void rptOrderDetail_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        // If we are binding the footer row, let's add in our total
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            var ltName      = (Literal)e.Item.FindControl("ltName");
            var ltTotalItem = (Literal)e.Item.FindControl("ltTotalItem");

            var productId   = ConvertUtility.ToInt32(((HiddenField)e.Item.FindControl("hfProduct")).Value);
            var quantity    = ConvertUtility.ToInt32(((TextBox)e.Item.FindControl("txtQuantity")).Text);

            var o = ProductController.Product_GetById(productId);
            if (o != null)
            {
                ltName.Text = string.Format("<a href=\"{0}\" class=\"colr\">{1}</a><p>{2}</p>",
                                                UrlHelpers.ProductDetail(CategoryController.GetCategoryName(o.CategoryId), o.ProductId, o.Name),
                                                o.Code, o.Name);

                if (productId > 0 && quantity > 0)
                    ltTotalItem.Text = Utils.FormatPrice(ConvertUtility.ToDecimal(o.UnitPrice * quantity));
            }
        }
    }

    protected void rptOrderDetail_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "RemoveOrderDetail")
        {
            var orderDetailId = Convert.ToInt32(e.CommandArgument);
            if (orderDetailId > 0)
            {
                OrderDetailController.OrderDetail_Delete(orderDetailId);

                ltMessage.Text = MessageUtility.ShowMessage(null, "Xóa chi tiết đơn hàng thành công.", MessageType.Information);                
            }
        }
        ListOrderDetailByOrderId();
    }    
}